package battleships;
/**
* Title: Deployment
* Description: Ship deployment class
* @main author: Jake Kooiman
* @email jake.kooiman@my.uwrf.edu
* @editors:
* @last update: 12/1/11
* 
*/

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;


public class Deployment {
	
	static ArrayList<String> position;
	
	
	
	
	////// creating the instance of the 5 ships

	
	/**
	 
	int newLength = 5;
	int newHeath = 5; 
	String newName = "Aircraft Carrier";
	ships Aircraft_carrier = new ships(newName, newLength, newHeath);
	
	int bat_length = 4; 
	String battleship;
	ships Battleship = new ships(battleship, bat_length, bat_length);
	
	int des_length = 3; 
	String destoryer;
	ships Destoryer = new ships(destoryer, des_length, des_length);

	int sub_length = 3;
	String submarine;
	ships Submarine = new ships(submarine, sub_length, sub_length);

	
	int patrol_Length = 2;
	String patrol;
	ships Partol = new ships(patrol, patrol_Length,patrol_Length );
	
	*/
	
	
	public static void main(String[] args) {
		
		
		

		final String directory= "src/";
		final String filename = "deployment1.xml";
		final String fullName = directory + filename;
		
		String name = null;
		int length = 0;
		String lgetter;
		
		
		ships[] Ships = new ships[5];
		
		File infile = new File( fullName );
		Document indoc = null;
		
		try {
			DocumentBuilder docbuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
			indoc = docbuilder.parse( infile );
		} catch (ParserConfigurationException e) {
			System.err.println("Not able to create DocumentBuilder object!");
			e.printStackTrace();
		} catch (SAXException e) {
			System.err.println("Not able to read XML file: " + fullName );
			e.printStackTrace();
		} catch (IOException e) {
			System.err.println("Not able to open file: " + fullName );
			e.printStackTrace();
		}
		
		XPath path = XPathFactory.newInstance().newXPath();
		final String inspectString_root = "count(/ship/ship)";
		
		System.out.println(" Reading in " + filename);
		try {
			int o = 0;
			int i = 1;
			int x = 0;
			int n = Integer.parseInt( path.evaluate(inspectString_root, indoc ));
			String[] pose = new String[4];
			while(i <= n){
				name = "/ship/ship["+i+"]/shipName";
				lgetter = "/ship/ship["+i+"]/shipLength";
				
				name = path.evaluate(name, indoc);
				length = Integer.parseInt( path.evaluate(lgetter, indoc ) );
				if(length == 5){
					for(x = 0; x<5; x++){
						String pgetter = "/ship/ship["+i+"]/position["+x+"]";
						pose[x] = path.evaluate(pgetter, indoc ) ;
					}
					Position(pose[1], pose[2], pose[3], pose[4], pose[5]);
				}
				if(length == 4){
					for(x = 0; x<4; x++){
						String pgetter = "/ship/ship["+i+"]/position["+x+"]";
						pose[x] = path.evaluate(pgetter, indoc ) ;
					}
					Position(pose[1], pose[2], pose[3], pose[4], null);
				}
				if(length == 3){
					for(x = 0; x<3; x++){
						String pgetter = "/ship/ship["+i+"]/position["+x+"]";
						pose[x] = path.evaluate(pgetter, indoc ) ;
					}
					Position(pose[1], pose[2], pose[3], null, null);
				}
				if(length == 2){
					for(x = 0; x<2; x++){
						String pgetter = "/ship/ship["+i+"]/position["+x+"]";
						pose[x] = path.evaluate(pgetter, indoc ) ;
					}
					Position(pose[1], pose[2], null, null, null);
				}
				
				Ships[o] = new ships(name, length, length);
				
				i++; o++;
			}
			
		}
		catch (NumberFormatException e) {
			System.err.println("Not able to read number.");
			e.printStackTrace();
		} 
		catch (XPathExpressionException e) {
			System.err.println("Bad XPath expression.");
			e.printStackTrace();
		}
		
	}
	public static void Position(String a, String b, String c, String d, String e){
		position = new ArrayList<String>();
		position.add(a); position.add(b); position.add(c); position.add(d); position.add(e);
	}
	
	public ArrayList<String> GetPositions(){
		return position;
	}

}
